home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Sessions / Traut / ZStrings / Source / Win32 / Win32ZStringToolsDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  12.3 KB  |  470 lines

  1. /*==================================================================
  2.     File:        Win32ZStringToolsDlg.cpp
  3.  
  4.     Contains:    Tool for creating override libraries for
  5.                 ZStrings on the  Windows
  6.  
  7.     Written by:    Nalini Prakash
  8.  
  9.     Copyright:    2000-2001 Connectix Corporation
  10.     
  11.     This source has been placed into the public domain by
  12.     Connectix Corporation. You have the right to modify, 
  13.     distribute or use this code without any legal limitations
  14.     or finanicial/licensing requirements. Connectix is not 
  15.     liable for any problems that result from the use of this 
  16.     code.
  17.     
  18.     If you have comments, feedback, questions, or would like
  19.     to submit bug fixes or updates to this code, please email
  20.     opensource@connectix.com.
  21. ==================================================================*/
  22.  
  23. #include "StdAfx.h"
  24.  
  25. #include "Win32ZStringTools.h"
  26. #include "Win32ZStringToolsDlg.h"
  27. #include "Win32ZStringExtractTool.h"
  28. #include "Win32ZStringCompareTool.h"
  29. #include "Win32ZStringOverrideTool.h"
  30.  
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36.  
  37.  
  38. char *    CWin32ZStringToolsDlg::sSourceFileTitle = "Please Select Source File";
  39. char *    CWin32ZStringToolsDlg::sCompareFileTitle = "Please Select Comparison File";
  40. char *    CWin32ZStringToolsDlg::sDestFileTitle = "Please Select Destination File";
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CAboutDlg dialog used for App About
  44.  
  45. class CAboutDlg : public CDialog
  46. {
  47. public:
  48.     CAboutDlg();
  49.  
  50. // Dialog Data
  51.     //{{AFX_DATA(CAboutDlg)
  52.     enum { IDD = IDD_ABOUTBOX };
  53.     //}}AFX_DATA
  54.  
  55.     // ClassWizard generated virtual function overrides
  56.     //{{AFX_VIRTUAL(CAboutDlg)
  57.     protected:
  58.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  59.     //}}AFX_VIRTUAL
  60.  
  61. // Implementation
  62. protected:
  63.     //{{AFX_MSG(CAboutDlg)
  64.     //}}AFX_MSG
  65.     DECLARE_MESSAGE_MAP()
  66. };
  67.  
  68. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  69. {
  70.     //{{AFX_DATA_INIT(CAboutDlg)
  71.     //}}AFX_DATA_INIT
  72. }
  73.  
  74. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  75. {
  76.     CDialog::DoDataExchange(pDX);
  77.     //{{AFX_DATA_MAP(CAboutDlg)
  78.     //}}AFX_DATA_MAP
  79. }
  80.  
  81. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  82.     //{{AFX_MSG_MAP(CAboutDlg)
  83.         // No message handlers
  84.     //}}AFX_MSG_MAP
  85. END_MESSAGE_MAP()
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CWin32ZStringToolsDlg dialog
  89.  
  90. CWin32ZStringToolsDlg::CWin32ZStringToolsDlg(CWnd* pParent /*=NULL*/)
  91.     : CDialog(CWin32ZStringToolsDlg::IDD, pParent), mCtrlCompareFile(false), 
  92.     mCtrlDestFile(false), mCtrlSourceFile(false)
  93. {
  94.     //{{AFX_DATA_INIT(CWin32ZStringToolsDlg)
  95.     mCompareFileName = _T("");
  96.     mDestFileName = _T("");
  97.     mSourceFileName = _T("");
  98.     mCategorizeOutput = TRUE;
  99.     mAllowTagSemicolon = FALSE;
  100.     mOutputTagFormat = kOutputNumericTag;
  101.     mFlagDuplicates = FALSE;
  102.     mOutputWarnings = TRUE;
  103.     mConvertHighASCIIChar = TRUE;
  104.     //}}AFX_DATA_INIT
  105.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  106.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  107. }
  108.  
  109. void 
  110. CWin32ZStringToolsDlg::DoDataExchange(CDataExchange* pDX)
  111. {
  112.     CDialog::DoDataExchange(pDX);
  113.     //{{AFX_DATA_MAP(CWin32ZStringToolsDlg)
  114.     DDX_Control(pDX, IDC_EDIT_SourceFileName, mCtrlSourceFile);
  115.     DDX_Control(pDX, IDC_EDIT_CompareFileName, mCtrlCompareFile);
  116.     DDX_Control(pDX, IDC_EDIT_DestFileName, mCtrlDestFile);
  117.     DDX_Text(pDX, IDC_EDIT_CompareFileName, mCompareFileName);
  118.     DDX_Text(pDX, IDC_EDIT_DestFileName, mDestFileName);
  119.     DDX_Text(pDX, IDC_EDIT_SourceFileName, mSourceFileName);
  120.     DDX_Check(pDX, IDC_CHECK_Categorize, mCategorizeOutput);
  121.     DDX_Check(pDX, IDC_CHECK_Semicolon, mAllowTagSemicolon);
  122.     DDX_Radio(pDX, IDC_RADIO_TagFormat, mOutputTagFormat);
  123.     DDX_Check(pDX, IDC_CHECK_FlagDuplicates, mFlagDuplicates);
  124.     DDX_Check(pDX, IDC_CHECK_ShowWarnings, mOutputWarnings);
  125.     DDX_Check(pDX, IDC_CHECK_ConvertHighASCII, mConvertHighASCIIChar);
  126.     //}}AFX_DATA_MAP
  127. }
  128.  
  129. BEGIN_MESSAGE_MAP(CWin32ZStringToolsDlg, CDialog)
  130.     //{{AFX_MSG_MAP(CWin32ZStringToolsDlg)
  131.     ON_WM_SYSCOMMAND()
  132.     ON_WM_PAINT()
  133.     ON_WM_QUERYDRAGICON()
  134.     ON_BN_CLICKED(IDExtract, OnExtract)
  135.     ON_BN_CLICKED(IDOverride, OnOverride)
  136.     ON_BN_CLICKED(IDOK, OnOk)
  137.     ON_EN_CHANGE(IDC_EDIT_CompareFileName, OnChangeEDITBoxes)
  138.     ON_BN_CLICKED(IDC_BrowseSrc, OnBrowseSource)
  139.     ON_BN_CLICKED(IDC_BrowseDest, OnBrowseDestination)
  140.     ON_BN_CLICKED(IDC_BrowseCmp, OnBrowseCompare)
  141.     ON_WM_CREATE()
  142.     ON_EN_CHANGE(IDC_EDIT_DestFileName, OnChangeEDITBoxes)
  143.     ON_EN_CHANGE(IDC_EDIT_SourceFileName, OnChangeEDITBoxes)
  144.     ON_BN_CLICKED(IDCompare, OnCompare)
  145.     //}}AFX_MSG_MAP
  146. END_MESSAGE_MAP()
  147.  
  148. /////////////////////////////////////////////////////////////////////////////
  149. // CWin32ZStringToolsDlg message handlers
  150.  
  151. BOOL 
  152. CWin32ZStringToolsDlg::OnInitDialog()
  153. {
  154.     CDialog::OnInitDialog();
  155.  
  156.     // Add "About..." menu item to system menu.
  157.  
  158.     // IDM_ABOUTBOX must be in the system command range.
  159.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  160.     ASSERT(IDM_ABOUTBOX < 0xF000);
  161.  
  162.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  163.     if (pSysMenu != NULL)
  164.     {
  165.         CString strAboutMenu;
  166.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  167.         if (!strAboutMenu.IsEmpty())
  168.         {
  169.             pSysMenu->AppendMenu(MF_SEPARATOR);
  170.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  171.         }
  172.     }
  173.  
  174.     // Set the icon for this dialog.  The framework does this automatically
  175.     //  when the application's main window is not a dialog
  176.     SetIcon(m_hIcon, TRUE);            // Set big icon
  177.     SetIcon(m_hIcon, FALSE);        // Set small icon
  178.  
  179.     UpdateData(FALSE);
  180.  
  181.     // Enable drag & drop
  182.     mCtrlSourceFile.DragAcceptFiles(TRUE);
  183.     mCtrlCompareFile.DragAcceptFiles(TRUE);
  184.     mCtrlDestFile.DragAcceptFiles(TRUE);
  185.  
  186.     mCtrlSourceFile.RegisterForDrop();
  187.     mCtrlCompareFile.RegisterForDrop();
  188.     mCtrlDestFile.RegisterForDrop();
  189.  
  190.     UpdateButtons();
  191.  
  192.     return TRUE;  // return TRUE  unless you set the focus to a control
  193. }
  194.  
  195. void 
  196. CWin32ZStringToolsDlg::OnSysCommand(UINT nID, LPARAM lParam)
  197. {
  198.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  199.     {
  200.         CAboutDlg dlgAbout;
  201.         dlgAbout.DoModal();
  202.     }
  203.     else
  204.     {
  205.         CDialog::OnSysCommand(nID, lParam);
  206.     }
  207. }
  208.  
  209. // If you add a minimize button to your dialog, you will need the code below
  210. //  to draw the icon.  For MFC applications using the document/view model,
  211. //  this is automatically done for you by the framework.
  212.  
  213. void 
  214. CWin32ZStringToolsDlg::OnPaint() 
  215. {
  216.     if (IsIconic())
  217.     {
  218.         CPaintDC dc(this); // device context for painting
  219.  
  220.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  221.  
  222.         // Center icon in client rectangle
  223.         int cxIcon = GetSystemMetrics(SM_CXICON);
  224.         int cyIcon = GetSystemMetrics(SM_CYICON);
  225.         CRect rect;
  226.         GetClientRect(&rect);
  227.         int x = (rect.Width() - cxIcon + 1) / 2;
  228.         int y = (rect.Height() - cyIcon + 1) / 2;
  229.  
  230.         // Draw the icon
  231.         dc.DrawIcon(x, y, m_hIcon);
  232.     }
  233.     else
  234.     {
  235.         CDialog::OnPaint();
  236.     }
  237. }
  238.  
  239. // The system calls this to obtain the cursor to display while the user drags
  240. //  the minimized window.
  241. HCURSOR CWin32ZStringToolsDlg::OnQueryDragIcon()
  242. {
  243.     return (HCURSOR) m_hIcon;
  244. }
  245.  
  246.  
  247. /*------------------------------------------------------------------
  248.     OnExtract
  249. ------------------------------------------------------------------*/
  250.  
  251. void
  252. CWin32ZStringToolsDlg::OnExtract() 
  253. {
  254.     UpdateData();
  255.     Win32ZStringExtractTool        extractTool;
  256.     ZToolOptions    options(mOutputTagFormat == kOutputNumericTag, mCategorizeOutput, 
  257.                             mAllowTagSemicolon, mFlagDuplicates, mOutputWarnings, 
  258.                             mConvertHighASCIIChar);
  259.     if (extractTool.ExtractZStrings(mSourceFileName, mDestFileName, options))
  260.     {
  261.         mSourceFileName = _T("");
  262.         mDestFileName = _T("");
  263.         UpdateData(FALSE);
  264.         UpdateButtons();
  265.     }
  266. }
  267.  
  268.  
  269. /*------------------------------------------------------------------
  270.     OnCompare
  271. ------------------------------------------------------------------*/
  272.  
  273. void 
  274. CWin32ZStringToolsDlg::OnCompare() 
  275. {
  276.     UpdateData();
  277.     Win32ZStringCompareTool        compareTool;
  278.     ZToolOptions    options(mOutputTagFormat==kOutputNumericTag, mCategorizeOutput,
  279.                             mAllowTagSemicolon, mFlagDuplicates, mOutputWarnings,
  280.                             mConvertHighASCIIChar);
  281.     if(compareTool.CompareZStrings(mSourceFileName, mCompareFileName, mDestFileName, options))
  282.     {
  283.         mSourceFileName = _T("");
  284.         mCompareFileName = _T("");
  285.         mDestFileName = _T("");
  286.         UpdateData(FALSE);
  287.         UpdateButtons();
  288.     }
  289. }
  290.  
  291.  
  292. /*------------------------------------------------------------------
  293.     OnOverride
  294. ------------------------------------------------------------------*/
  295.  
  296. void 
  297. CWin32ZStringToolsDlg::OnOverride() 
  298. {
  299.     UpdateData();
  300.     Win32ZStringOverrideTool        overrideTool;
  301.     ZToolOptions    options(mOutputTagFormat==kOutputNumericTag, mCategorizeOutput,
  302.                             mAllowTagSemicolon, mFlagDuplicates, mOutputWarnings,
  303.                             mConvertHighASCIIChar);
  304.     options.mPrintErrorsOnly = true;
  305.     if(overrideTool.CreateOverrideDictionary(mSourceFileName, mDestFileName, options))
  306.     {
  307.         mSourceFileName = _T("");
  308.         mDestFileName = _T("");
  309.         UpdateData(FALSE);
  310.         UpdateButtons();
  311.     }
  312. }
  313.  
  314.  
  315. /*------------------------------------------------------------------
  316.     OnOk
  317. ------------------------------------------------------------------*/
  318.  
  319. void 
  320. CWin32ZStringToolsDlg::OnOk() 
  321. {
  322.     EndDialog(IDOK);
  323. }
  324.  
  325.  
  326. /*------------------------------------------------------------------
  327.     OnCancel
  328. ------------------------------------------------------------------*/
  329.  
  330. void
  331. CWin32ZStringToolsDlg::OnCancel() 
  332. {
  333.     EndDialog(IDCANCEL);    
  334.     CDialog::OnCancel();
  335. }
  336.  
  337. /*------------------------------------------------------------------
  338.     UpdateButtons
  339. ------------------------------------------------------------------*/
  340.  
  341. void 
  342. CWin32ZStringToolsDlg::UpdateButtons()
  343. {
  344.     CWnd* bExtract;
  345.     CWnd* bCompare;
  346.     CWnd* bOverride;
  347.  
  348.     CString    src, dest;
  349.  
  350.     bExtract = GetDlgItem(IDExtract);
  351.     bOverride = GetDlgItem(IDOverride);
  352.     bCompare = GetDlgItem(IDCompare);
  353.  
  354.     if (!bExtract || !bOverride || !bCompare)
  355.         goto Error;
  356.  
  357.     bCompare->EnableWindow(FALSE);
  358.     bExtract->EnableWindow(FALSE);
  359.     bOverride->EnableWindow(FALSE);    
  360.  
  361.     // Extract and override require a source and a destination file.
  362.     if (!mSourceFileName.IsEmpty() && !mDestFileName.IsEmpty()) 
  363.     {
  364.         src = mSourceFileName;
  365.         src.MakeLower();
  366.         dest = mDestFileName;
  367.         dest.MakeLower();
  368.  
  369.         if (!mCompareFileName.IsEmpty()) 
  370.             bCompare->EnableWindow(TRUE);
  371.         else if ((src.Find(".exe")!=-1 || src.Find(".html")!=-1) && dest.Find(".html")!=-1) 
  372.             bExtract->EnableWindow(TRUE);
  373.         else if (dest.Find(".dict")!=-1) 
  374.             bOverride->EnableWindow(TRUE);    
  375.     }
  376.  
  377. Error:
  378.     return;
  379. }
  380.  
  381. /*------------------------------------------------------------------
  382.     OnChangeEDITBoxes
  383. ------------------------------------------------------------------*/
  384.  
  385. void 
  386. CWin32ZStringToolsDlg::OnChangeEDITBoxes() 
  387. {
  388.     UpdateData();
  389.      UpdateButtons();
  390. }
  391.  
  392.  
  393. /*------------------------------------------------------------------
  394.     GetFileName
  395. ------------------------------------------------------------------*/
  396.  
  397. CString
  398. CWin32ZStringToolsDlg::GetFileName(
  399.     char *        dialogTitle) 
  400. {
  401.     // Ask for the new file name.
  402.     static char *    filterBinary = "Binary Files (*.exe)|*.exe|HTML Files (*.html)|*.html|Dictionary Files (*.dict)|*.dict|All Files (*.*)|*.*||";
  403.     char *    extension = "exe";
  404.  
  405.     CFileDialog fileDlg(true, extension, (LPCTSTR)NULL,
  406.         OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, filterBinary);
  407.  
  408.     // Change title of Standard Open file dialog.
  409.     fileDlg.m_ofn.lStructSize = sizeof (OPENFILENAME);
  410.     fileDlg.m_ofn.lpstrTitle = dialogTitle;
  411.     if (fileDlg.DoModal() != IDOK)
  412.         return _T("");
  413.  
  414.     return fileDlg.GetPathName();
  415. }
  416.  
  417. /*------------------------------------------------------------------
  418.     OnBrowseSource
  419. ------------------------------------------------------------------*/
  420.  
  421. void 
  422. CWin32ZStringToolsDlg::OnBrowseSource() 
  423. {
  424.     UpdateData();
  425.     mSourceFileName = GetFileName(sSourceFileTitle);
  426.     UpdateData(FALSE);
  427.     UpdateButtons();
  428. }
  429.  
  430. /*------------------------------------------------------------------
  431.     OnBrowseCompare
  432. ------------------------------------------------------------------*/
  433.  
  434. void 
  435. CWin32ZStringToolsDlg::OnBrowseCompare() 
  436. {
  437.     UpdateData();
  438.     mCompareFileName = GetFileName(sCompareFileTitle);
  439.     UpdateData(FALSE);
  440.     UpdateButtons();
  441. }
  442.  
  443.  
  444. /*------------------------------------------------------------------
  445.     OnBrowseDestination
  446. ------------------------------------------------------------------*/
  447.  
  448. void 
  449. CWin32ZStringToolsDlg::OnBrowseDestination() 
  450. {
  451.     // Ask for the name of the output file.
  452.     char *    filterReport = "HTML Files (*.html)|*.html|Dictionary Files (*.dict)|*.dict|All Files (*.*)|*.*||";
  453.     const char *    outExtension = "html";
  454.  
  455.     CFileDialog outDlg(false, outExtension, NULL,
  456.         OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, filterReport, NULL);
  457.  
  458.     // Change title of Standard Save file dialog.
  459.     outDlg.m_ofn.lpstrTitle = sDestFileTitle;
  460.     if (outDlg.DoModal() != IDOK)
  461.         return;
  462.  
  463.     UpdateData();
  464.     mDestFileName = outDlg.GetPathName();
  465.     UpdateData(FALSE);
  466.     UpdateButtons();
  467. }
  468.  
  469.  
  470.